
var commonUtil = {
    //请求json文件
    getJson: function (url, data, callBack) {
        $.ajax({
            url: url,
            data: data,
            async: false,
            success: function (result) {
                callBack(result);
            }
        });
    },
    //限制输入字数
    inputLimitForId: function (element, num) {
        var limitEl = document.getElementById(element);
        limitEl.addEventListener('input', function () {
            if (limitEl.value.length > num)
                limitEl.value = limitEl.value.substring(0, num);
        }, false);
    },
    //键盘值 验证带小数点的小数
    checkKeyCode: function (obj, event) {
        event = event || window.event;
        if (!(event.keyCode == 46) && !(event.keyCode == 8) && !(event.keyCode == 37) && !(event.keyCode == 39)) {
            if (!((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) {
                return false;
            } else {
                return true;
            }
        } else { return true; }

    },
    //获取按钮
    loadBtnList: function (ModuleID, deal) {
        Y.API2('Right', 'GetBtnRight', { ModuleID: ModuleID }, function (data) {
            var _html = "";
            var loginJson = window.sessionStorage.getItem('login-info');
            var btnStyle = 'btn-Style1';
            if (!Y.checkNull(loginJson)) {
                loginJson = JSON.parse(loginJson);
                loginJson.ConfigList.map(function (item) {
                    if (item.dictValue == 'btn-Style')
                        btnStyle = item.Value;
                })
            }
            data.map(function (item) {
                //获取按钮样式
                _html += '<button type="button" id="' + item.BtnID + '" class="btn ' + (btnStyle == 'btn-Style1' ? '' : 'btn-outline ') + item.BackColor + '" data-toggle="modal" data-target="#' + item.MoldID + '">';
                if (!Y.checkNull(item.Icon) && btnStyle == 'btn-Style1') {
                    _html += '<i class="' + item.Icon + '"></i>&nbsp;&nbsp;' + item.BtnName;
                } else {
                    _html += item.BtnName;
                }
                _html += '</button>';
            });
            $(".btns-container").append(_html);
            deal();
        });
    },
    //关闭重复窗口
    closeRepeatWindow: function (name) {
        var jmt = $('.J_menuTabs .J_menuTab', window.parent.document);
        jmt.each(function () {
            var temp = $(this).text().trim();
            if (temp == name)
                $(this).find('i').click();
        });
    },
    loadJson: function (type, control) {
        this.getJson('../assets/json/terminalparam.json', {}, function (data) {
            //弹窗绑定部分
            var arr = new Array();
            data.map(function (item) {
                if (item.type == type) {
                    arr.push(item);
                }
            })
            var html = '';
            arr.map(function (item) {
                html += '<option value="' + item.id + '">' + item.text + '</option>';
            });
            $("#" + control).append(html);
        });
    },
    loadDict: function (param, deal) {
        Y.API2('SysDict', 'GetList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                deal(data);
            }
        })
    },
    loadDict2: function (param, deal) {
        Y.API3('SysDict', 'GetList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                deal(data);
            }
        })
    },
    bindDict: function (param, els) {
        Y.API2('SysDict', 'GetList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.DictValue != param.DictValue)
                        html += '<option value=' + item.DictName + '>' + item.DictName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    bindDict2: function (param, els) {
        Y.API3('SysDict', 'GetList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.DictValue != param.DictValue)
                        html += '<option value=' + item.DictName + '>' + item.DictName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    bindDict3: function (param, els) {
        Y.API3('SysDict',
            'GetList',
            param,
            function (data) {
                if (data.Success == false) {
                    swal(data.Message, '', 'error');
                } else {
                    var html = '';
                    data.map(function (item) {
                        if (item.DictValue != param.DictValue)
                            html += "<label class=\"effective\"><input value=" + item.DictValue + " type=\"checkbox\" >" + item.DictName + "</label>";
                    });
                    $(els).append(html);
                }
            });
    },
    //绑定数据参数
    bindDwGlobalVariables: function (param, els) {
        Y.API3('DwGlobalVariable', 'GetGlobalVariableList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.dictValue != param.dictValue)
                        html += '<option value=' + item.dictValue + '>' + item.dictName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    bindDwGlobalVariables2: function (param, els, callback) {
        Y.API2('DwGlobalVariable', 'GetGlobalVariableList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.dictValue != param.dictValue)
                        html += '<option value=' + item.dictValue + '>' + item.dictName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
                if (typeof (callback) == 'function')
                    callback();
            }
        })
    },

    bindOdsErpBasStocks: function (param, els) {
        Y.API3('OdsErpBasArea', 'GetAllOdsErpBasStockList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.StockID != param.StockID)
                        html += '<option value=' + item.StockID + '>' + item.StockName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    //配货仓库
    bindOdsErpBasStocks2: function (param, els) {
        Y.API2('OdsErpBasArea', 'GetAllOdsErpBasStockList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.StockID != param.StockID)
                        html += '<option value=' + item.StockID + '>' + item.StockName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    //配货仓库
    bindOdsErpBasStocks3: function (param, els) {
        Y.API2('PickingOperations', 'GetSendStockList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.StockID != param.StockID)
                        html += '<option value=' + item.StockID + '>' + item.StockName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    loadSelectJson: function (url, type, deal) {
        var tempList = [];
        this.getJson(url, {}, function (data) {
            data.map(function (item) {
                if (item.type == type) {
                    item.select = false;
                    tempList.push(item);
                }
            });
            deal(tempList);
        });
    },
    initDeleteBtnEvent: function (removeInfo) {
        $('.btns-container').on("click", "#deleteBtn", function () {
            swal({
                title: "您确定要删除这条信息吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "删除",
                cancelButtonText: "取消",
                closeOnConfirm: false
            }, function () {
                removeInfo.callBack();
            });
        });
    },

    // 返回类别信息
    GetOdsErpSdMatKind: function (els, callback) {
        Y.API3('DwGlobalVariable', 'GetOdsErpSdMatKind', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    GetOdsErpSdMatKind2: function (els, callback) {
        Y.API2('DwGlobalVariable', 'GetOdsErpSdMatKind', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },

    // 类别,绑定到select控件
    bindOdsErpSdMatKind: function (els, callback) {
        this.GetOdsErpSdMatKind(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.KindID + '>' + item.KindName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },
    bindOdsErpSdMatKind2: function (els, callback) {
        this.GetOdsErpSdMatKind2(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.KindID + '>' + item.KindName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },
    // 类别,绑定到select控件 过滤后绑定（鞋、服装、配件） 时间：2017-04-26 曹亚亮
    bindOdsErpSdMatKind3: function (els, callback) {
        this.GetOdsErpSdMatKind2(els, function (data) {
            var html = '';
            data.map(function (item) {
                if (item.KindID == "000001" || item.KindID == "000002" || item.KindID == "000003") {
                    html += '<option value=' + item.KindID + '>' + item.KindName + '</option>';
                }
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },

    // 返回系列信息
    GetOdsErpSdMatSeries: function (els, callback) {
        Y.API3('DwGlobalVariable', 'GetOdsErpSdMatSeries', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    GetOdsErpSdMatSeries2: function (els, callback) {
        Y.API2('DwGlobalVariable', 'GetOdsErpSdMatSeries', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },

    // 系列,绑定到select控件
    bindOdsErpSdMatSeries: function (els, callback) {
        this.GetOdsErpSdMatSeries(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.SeriesID + '>' + item.SeriesName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },
    bindOdsErpSdMatSeries2: function (els, callback) {
        this.GetOdsErpSdMatSeries2(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.SeriesID + '>' + item.SeriesName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },



    // 返回品类信息
    GetOdsErpSDMatModel: function (els, callback) {
        Y.API3('DWSuppleAllMessage', 'GetOdsErpSDMatModel', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    GetOdsErpSDMatModel2: function (els, callback) {
        Y.API2('DWSuppleAllMessage', 'GetOdsErpSDMatModel', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },

    // 品类,绑定到select控件
    bindOdsErpSDMatModel: function (els, callback) {
        this.GetOdsErpSDMatModel(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.ModelID + '>' + item.ModelName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },
    bindOdsErpSDMatModel2: function (els, callback) {
        this.GetOdsErpSDMatModel2(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.ModelID + '>' + item.ModelName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },
    bindGlobalVariableListShop: function (param, els, callback) {
        Y.API2('DwGlobalVariable', 'GetGlobalVariableListShop', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.InfoID != param.InfoID)
                        html += '<option value=' + item.InfoID + '>' + item.InfoName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
                if (typeof (callback) == 'function')
                    callback();
            }
        })
    },

    // 返回小品类信息
    GetOdsErpSDMatItem: function (els, callback) {
        Y.API3('DwGlobalVariable', 'GetOdsErpSDMatItem', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },

    // 小品类,绑定到select控件
    bindOdsErpSdMatSeries: function (els, callback) {
        this.GetOdsErpSDMatItem(els, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.ItemID + '>' + item.ItemName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(data);
            }
        });
    },
    // 获取基本数据类型:企划中使用(数据类型:1-类别;2-系列;3-商品层;4-品类)
    GetDropDownList: function (param, callback) {
        Y.API2('BaseInfo', 'GetDropDownList', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    //基本数据类型 绑定到select控件 category
    bindGetDropDownList: function (els, type, callback) {
        this.GetDropDownList({ type: type }, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.ConID + '>' + item.ConName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {

                callback.apply(els, data);
            }
        });
    },
    // 获取小品类:企划中使用
    GetDropDownItem: function (param, callback) {
        Y.API2('BaseInfo', 'GetDropDownItem', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    //获取小品类 绑定到select控件
    bindDropDownItem: function (els, ModelID, callback) {
        this.GetDropDownItem({ ModelID: ModelID }, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.ItemID + '>' + item.ItemName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback.apply(els, data);
            }
        });
    },

    // 周:企划中使用
    GetWeekItem: function (param, callback) {
        Y.API2('PLBasSalePlan', 'GetWeekByYearAndMonth', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    //周 绑定到select控件
    bindWeekItem: function (els, callback) {
        this.GetWeekItem({}, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.Weekv + '>' + item.Weekv + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(els, data);
            }
        });
    },
    // 零售区域列表
    GetBasOperationArea: function (param, callback) {
        Y.API2('DwShop', 'GetBasOperationArea', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        })
    },
    //零售区域列表 绑定到select控件
    bindBasOperationArea: function (els, callback) {
        this.GetBasOperationArea({}, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.OperationAreaName + '>' + item.OperationAreaName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(els, data);
            }
        });
    },
    //零售区域列表 绑定到select控件
    bindBasOperationArea2: function (els, callback) {
        this.GetBasOperationArea({}, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.OperationAreaID + '>' + item.OperationAreaName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(els, data);
            }
        });
    },
    // 零售区域列表（加入登录用户的店铺权限）
    GetBasOperationArea2: function (param, callback) {
        Y.API2('DwShop', 'GetBasOperationArea2', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                callback(data);
            }
        });
    },
    //零售区域列表 绑定到select控件（加入登录用户的店铺权限）
    bindBasOperationArea3: function (els, callback) {
        this.GetBasOperationArea2({}, function (data) {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item.OperationAreaID + '>' + item.OperationAreaName + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
            });
            if (typeof (callback) != "undefined") {
                callback(els, data);
            }
        });
    },
    //获取季节下拉框数据:企划中使用
    bindSeasonList: function (param, els) {
        Y.API3('BaseInfo', 'GetSeasonDropDown', param, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    if (item.dictValue != param.dictValue)
                        html += '<option value=' + item.SeasonID + '>' + item.SeasonName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        })
    },
    // 获取尺码类型列表
    getSizeCodeTypeList: function (els) {
        Y.API2('SizeType', 'GetSizeCodeTypeList', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    html += '<option value=' + item.SizeCodeType + '>' + item.SizeCodeType + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
            }
        });
    },
    // 获取尺码列表
    getSizeList: function (els, sizeCodeType, callback) {
        Y.API2('SizeType', 'GetSizeList', { sizeCodeType: sizeCodeType }, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    html += '<option value=' + item.SizeID + '>' + item.SizeName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
                if (typeof callback === "function") {
                    callback();
                }
            }
        });
    },
    // 获取公司列表
    getCompanyList: function (els, callback) {
        Y.API2('Dept', 'GetDeptList', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    html += '<option value=' + item.CompanyID + '>' + item.DeptContent + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
                if (typeof callback === "function") {
                    callback();
                }
            }
        });
    },
    // 获取性别列表
    getSexList: function (els, callback) {
        Y.API2('DwGlobalVariable', 'GetOdsErpSDMatSex', {}, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.map(function (item) {
                    html += '<option value=' + item.SexID + '>' + item.SexName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
                if (typeof callback === "function") {
                    callback();
                }
            }
        });
    },
    //绑定店铺销售级别到Select
    getShopLevels: function (els, callback) {
        Y.API2('SysDict', 'GetList', { DictValue: "shoplevel" }, function (data) {
            if (data.Success == false) {
                swal(data.Message, '', 'error');
            } else {
                var html = '';
                data.filter(function (item) {
                    return item.DictValue != "shoplevel";
                }).map(function (item) {
                    html += '<option value=' + item.DictValue + '>' + item.DictName + '</option>';
                });
                els.map(function (item) {
                    $(item).append(html);
                });
                if (typeof callback === "function") {
                    callback();
                }
            }
        });
    },
    //自动收缩列表宽度（如 智能补货结果） false 时可以禁用
    autoLeftWidth: function (obj) {
        if (obj == false || obj == "false") return;
        $('.navbar-minimalize i', window.parent.document).click();
    },
    //自动替换输入值，默认为不能超过100(99.9)，保留一位小数， obj-要检验的值，MaxValue-最大值，decNum-保留的小数位
    ChangNumValue: function (obj, MaxValue, decNum) {
        if (parseFloat(MaxValue) == "" || MaxValue == undefined || MaxValue == "") MaxValue = 100;
        if (parseFloat(decNum) == "" || decNum == undefined || decNum == "") decNum = 2;//默认保留两位小数
        decNum = parseFloat(decNum) + 1;//取的是小数点位数，需要加1
        if (obj != undefined && obj != "" && /[^0-9\.]/g.test(obj)) {
            obj = obj.toString().replace(/[^0-9\.]/g, '');//替换非数字和点
            var lastCount = obj.indexOf('.');
            if (lastCount != -1) {
                var lastValue = obj.charAt(lastCount + decNum);//小数点后第二位数
                obj = obj.substring(0, obj.indexOf('.') + decNum);
                if (lastValue != undefined && lastValue >= 5) //四舍五入
                    obj = parseFloat(obj.substring(0, obj.indexOf('.') + decNum)) + 0.1;
            }
        }
        if (parseFloat(obj) >= parseFloat(MaxValue))
            obj = parseFloat(MaxValue) - 0.1;
        if (MaxValue != undefined && obj.length > MaxValue.length + decNum)
            checkValue(obj.substring(0, MaxValue.length + decNum + 2), MaxValue, decNum);//如果出现精度问题，则截取内容(最大值长度+限定保留小数位+2位)重新运行
        return obj;
    }

}
var GetBtnJson = function (url, data, callBack) {
    $.ajax({
        type: "Get",
        url: url,

        data: data,
        async: false,
        dataType: 'json',
        success: function (result) {
            callBack(result);
        }
    });
}

//要提取到公共类
var nmCommonBindList = function (controller, action, params, els, value, text, size, callback) {
    Y.API2(controller, action, params, function (data) {
        if (data.Success == false) {
            swal(data.Message, '', 'error');
        } else {
            var html = '';
            data.map(function (item) {
                html += '<option value=' + item[value] + '>' + item[text] + '</option>';
            });
            els.map(function (item) {
                $(item).append(html);
                $(item).selectpicker({ size: size || 8, 'title': '请选择' });
                $(item).selectpicker('refresh');
            });
            if (typeof callback === "function") {
                callback();
            }
        }
    });
}

